<template>
  <div class="dl-table">
    <div class="header">
      <slot name="header">
        <div class="title">
          {{ title }}
        </div>
        <div class="handler">
          <slot name="headHandler"></slot>
        </div>
      </slot>
    </div>
    <el-table
      :data="listData"
      border
      style="width: 100%"
      @selection-change="handleSelectionChange"
      v-bind="childrenProps"
    >
      <el-table-column
        v-if="showSelectColmun"
        type="selection"
        align="center"
        width="60px"
      ></el-table-column>
      <el-table-column
        v-if="showIndex"
        type="index"
        width="80px"
        label="序号"
        align="center"
      ></el-table-column>
      <template v-for="item in propList" :key="item.prop">
        <el-table-column v-bind="item" align="center" show-overflow-tooltip>
          <template #default="scope">
            <slot :name="item.slotName" :row="scope.row">
              {{ scope.row[item.prop] }}
            </slot>
          </template>
        </el-table-column>
      </template>
    </el-table>
    <div class="footer" v-show="showFooter">
      <slot name="footer">
        <el-pagination
          :currentPage="pageInfo.currentPage"
          :page-size="pageInfo.pageSize"
          :page-sizes="[10, 20, 50]"
          layout="total, sizes, prev, pager, next, jumper"
          :total="listCount"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </slot>
    </div>
  </div>
</template>

<script lang="ts">
import { defineComponent } from "vue";
export default defineComponent({
  props: {
    childrenProps: {
      type: Object,
      default: {},
    },
    listCount: {
      type: Number,
      default: 0,
    },
    title: {
      type: String,
      default: "Header",
    },
    listData: {
      type: Array,
      required: true,
    },
    propList: {
      type: Array,
      required: true,
    },
    showIndex: {
      type: Boolean,
      default: false,
    },
    showSelectColmun: {
      type: Boolean,
      default: false,
    },
    pageInfo: {
      type: Object,
      default: () => ({ currentPage: 1, pageSize: 10 }),
    },
    showFooter: {
      type: Boolean,
      default: true,
    },
  },
  // emits: ["selectionChange", "update:page"],
  emits: ["selectionChange"],
  setup(prop, { emit }) {
    const handleSelectionChange = (value: any) => {
      emit("selectionChange", value);
    };
    const handleSizeChange = (pageSize: number) => {
      // emit("update:page", { ...prop.page, pageSize });
      prop.pageInfo.pageSize = pageSize;
      console.log("改变了pageSize", pageSize);
    };
    const handleCurrentChange = (currentPage: number) => {
      // emit("update:page", { ...prop.page, currentPage });
      prop.pageInfo.currentPage = currentPage;
      console.log("改变了currentPage", currentPage);
    };

    return {
      handleSelectionChange,
      handleSizeChange,
      handleCurrentChange,
    };
  },
});
</script>

<style scoped lang="less">
.dl-table {
  .header {
    display: flex;
    height: 45px;
    padding: 0 5px;
    justify-content: space-between;
    align-items: center;

    .title {
      font-size: 20px;
      font-weight: 700;
    }

    .handler {
      align-items: center;
    }
  }

  .footer {
    position: relative;
    margin-top: 50px;
    .el-pagination {
      text-align: right;
      position: absolute;
      right: 5px;
      bottom: 10px;
    }
  }
}
</style>
